<template >
  <div class="autoReplyEdit">
    <div class="autoReplyEditContent">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="文本" prop="text">
          <el-input v-model="ruleForm.text"></el-input>
        </el-form-item>
        <el-form-item label="资源" prop="eventkey">
          <div class="regionWrapper">
            <div class="nowSource" @click="handleSelectExitSource">
              <div v-if="showSourceName" class="sourceName">
                <span>资源名: {{ sourceName }}</span>
              </div>
              <svg-icon icon-class="plus" class="homeIcon"/>
              <p v-if="shouConnectionSource">
                关联资源
              </p>
            </div>
            <div class="addSource" @click="handleGoAddSource(null)">
              <svg-icon icon-class="plus" class="homeIcon"/>
              <p>
                添加新资源
              </p>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="ruleForm.status" label="normal">正常</el-radio>
          <el-radio v-model="ruleForm.status" label="disable">隐藏</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button v-if="this.$route.query.id == null" type="primary" @click="submitForm('ruleForm')">新增</el-button>
          <el-button v-else type="primary" @click="submitForm('ruleForm')">编辑</el-button>
          <el-button v-if="this.$route.query.id == null" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 嵌套表格的已经存在资源 -->
    <el-dialog :visible.sync="dialogTableVisible" title="资源选择" width="70%">
      <div class="searchContent">
        <el-form :inline="true" :model="queryForm" size="mini">
          <el-form-item label="资源标题">
            <el-input v-model="queryForm.sourceTitle" :min="0" :max="10" controls-position="right" style="width: 150px;"></el-input>
          </el-form-item>
          <el-form-item label="资源类型">
            <el-select v-model="queryForm.sourceType" placeholder="请选择资源类型">
              <el-option label="文本" value="text"></el-option>
              <el-option label="图文" value="news"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button type="warning" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-table :data="gridData">
          <el-table-column property="type" label="类型"></el-table-column>
          <el-table-column property="title" label="资源标题"></el-table-column>
          <el-table-column property="eventkey" label="事件标识" show-overflow-tooltip></el-table-column>
          <el-table-column property="statusStr" label="状态" width="50"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="clickAutoDetail(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 底部分页 -->
        <div class="bottom-pagination">
          <el-pagination
            :current-page.sync="pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            background
            @size-change="handleSizeChange"
            @current-change="getSourceList">
          </el-pagination>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {
  },
  data() {
    return {
      queryForm: {
        sourceTitle: '',
        sourceType: ''
      },
      shouConnectionSource: true,
      showSourceName: false,
      sourceName: '',
      // 分页参数
      pageNum: 1,
      pageSize: 5,
      total: 50,
      dialogTableVisible: false,
      ruleForm: {
        title: '',
        text: '',
        eventkey: '',
        remark: '',
        status: 'normal'
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        text: [
          { required: true, message: '请输入文本', trigger: 'blur' }
        ],
        eventkey: [
          { required: true, message: '请选择资源', trigger: 'blur' }
        ]
      },
      gridData: []
    }
  },
  created() {
    if (this.$route.query.id !== null) {
      this.getAutoreplyDetailById()
      this.showSourceName = true
      this.shouConnectionSource = false
    }
  },
  methods: {
    // 根据id获取详情页面
    getAutoreplyDetailById() {
      this.$http.get('/AutoReply/get?id=' + this.$route.query.id).then(response => {
        if (response.data.code === 0) {
          this.sourceName = response.data.data.responseTitle
          this.ruleForm = response.data.data
        }
      }).catch(error => {
        this.$message({
          showClose: true,
          message: '获取自动回复详情信息异常{' + error + '}',
          type: 'error'
        })
      })
    },
    // 获取关联已有资源
    getSourceList() {
      this.$http.get('/Response/list?offset=' + (this.pageNum - 1) * this.pageSize + '&limit=' + this.pageSize + '&title=' + this.queryForm.sourceTitle + '&type=' + this.queryForm.sourceType).then(response => {
        if (response.data.code === 0) {
          response.data.data.rows.forEach(element => {
            if (element.status === 'normal') {
              element.statusStr = '正常'
            } else {
              element.statusStr = '隐藏'
            }
          })
          // 数据总数赋值
          this.total = response.data.data.total
          this.gridData = response.data.data.rows
        }
      }).catch(error => {
        this.$message({
          showClose: true,
          message: '获取资源列表异常{' + error + '}',
          type: 'error'
        })
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 编辑行为
          if (this.$route.query.id && this.ruleForm.id) {
            // 编辑自动回复
            this.$http.post('/AutoReply/update', this.ruleForm).then(response => {
              if (response.data.code === 0) {
                this.$message({
                  showClose: true,
                  message: '编辑自动回复成功！',
                  type: 'success'
                })
              }
            }).catch(error => {
              this.$message({
                showClose: true,
                message: '编辑自动回复异常{' + error + '}',
                type: 'error'
              })
            })
          } else {
            // 添加自动回复
            this.$http.post('/AutoReply/add', this.ruleForm).then(response => {
              if (response.data.code === 0) {
                this.$message({
                  showClose: true,
                  message: '添加自动回复成功！',
                  type: 'success'
                })
              }
            }).catch(error => {
              this.$message({
                showClose: true,
                message: '添加自动回复异常{' + error + '}',
                type: 'error'
              })
            })
          }
        } else {
          this.$message({
            showClose: true,
            message: '表单验证失败！',
            type: 'error'
          })
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleSelectExitSource() {
      this.getSourceList()
      this.dialogTableVisible = true
    },
    handleGoAddSource(source) {
      this.$router.push({
        path: '/weixin/sourceedit',
        query: {
          id: source && source.id
        }
      })
    },
    // 分页
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.pageNum = 1
      this.getSourceList()
    },
    clickAutoDetail(value) {
      this.sourceName = value.title
      // 关联资源id
      this.ruleForm.eventkey = value.eventkey
      this.dialogTableVisible = false
      this.showSourceName = true
      this.shouConnectionSource = false
    },
    handleQuery() {
      this.getSourceList()
    },
    handleReset() {
      this.queryForm.sourceTitle = ''
      this.queryForm.sourceType = ''
      this.getSourceList()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import 'src/styles/variables.scss';
.autoReplyEdit {
   padding: 10px;
  .autoReplyEditContent {
    padding: 20px;
    background-color: white;
    .regionWrapper {
      display: flex;
      .nowSource {
        position: relative;
        cursor: pointer;
        width: 240px;
        height: 120px;
        border: 2px dotted #d9dadc;
        text-align: center;
        margin-bottom: 20px;
        .sourceName {
          width: 100%;
          height: 38px;
          background-color: #f4f5f9;
          span {
            line-height: 40px;
          }
        }
        .homeIcon {
          width: 30px;
          height: 30px;
          margin-top: 20px;
        }
        p {
          position: absolute;
          top: 40px;
          left: 90px;
          color: $color-primary;
        }
      }
      .addSource {
        position: relative;
        cursor: pointer;
        margin-left: 10px;
        width: 240px;
        height: 120px;
        border: 2px dotted #d9dadc;
        text-align: center;
        margin-bottom: 20px;
        .homeIcon {
          margin-top: 20px;
          width: 30px;
          height: 30px;
        }
        p {
          color: $color-primary;
          position: absolute;
          top: 40px;
          left: 85px;
        }
      }
    }
  }
}
.bottom-pagination {
  margin-top: 20px;
}
</style>
